package com.ccjq.spring.version.four.webmvc.controller;

import com.ccjq.spring.version.four.webmvc.domain.User;
import com.ccjq.spring.version.four.webmvc.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * describe:用户业务控制器
 *
 * @author xugang
 * @date 2019/1/15 15:53
 */
@Controller
@RequestMapping("/user")
public class UserController {
    private static final Log logger = LogFactory.getLog(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * Hello World练习
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/hello")
    public String hello(HttpServletRequest request, HttpServletResponse response) {
        logger.info("####-------------->>> UserController.hello");
        return "hello/hello";
    }

    /**
     * 跳转到注册页面
     *
     * @return
     */
    @RequestMapping("/register")
    public String register() {
        logger.info("####-------------->>> UserController.register");
        return "user/register";
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ModelAndView createUser(User user) {
        logger.info("####-------------->>> UserController.create");
        userService.saveUser(user);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user/createSuccess");
        modelAndView.addObject("user", user);
        return modelAndView;
    }
}
